package cc.miwuya.property.controller;

import cc.miwuya.common.util.Result;
import cc.miwuya.core.BaseController;
import cc.miwuya.property.domain.Parking;
import cc.miwuya.property.service.ParkingService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * @Author 迷雾森林
 * @Date 2024-02-27 14:53
 * @Desc 车位控制器
 */
@RestController
@RequestMapping("/property/parking")
public class ParkingController extends BaseController {
    @Resource
    ParkingService parkingService;

    /**
     * 添加或修改车位信息
     */
    @PostMapping("/edit")
    public Result edit(@RequestBody Parking parking){
        parkingService.saveOrUpdate(parking);
        return Result.success();
    }

    /**
     * 根据车位ID获取车位信息
     */
    @GetMapping("/info/{parkingId}")
    public Result info(@PathVariable Long parkingId){
        return Result.success(parkingService.info(parkingId));
    }

    /**
     * 获取车位列表
     */
    @GetMapping("/list/{residentialId}")
    public Result list(String name, @PathVariable Long residentialId){
        return Result.success(parkingService.getPage(getPage(),residentialId,name));
    }
    /**
     * 获取车位列表
     */
    @GetMapping("/list")
    public Result list(String name){
        return Result.success(parkingService.getPage(getPage(),name));
    }

    /**
     * 根据业主ID获取车位列表
     */
    @GetMapping("/list/owner/{ownerId}")
    public Result listByOwner(@PathVariable Long ownerId,@RequestParam("name") String name){
        return Result.success(parkingService.getOwnerParkingList(getPage(),ownerId,name));
    }

    /**
     * 根据车位ID回收车位
     */
    @GetMapping("/recover")
    public Result recover(Long parkingId){
        Parking parking = parkingService.getById(parkingId);
        Parking park = new Parking();
        park.setId(parkingId);
        park.setResidentialId(parking.getResidentialId());
        park.setAddress(parking.getAddress());
        park.setUpdated(LocalDateTime.now());
        park.setStatus(0);
        parkingService.removeById(parking);
        parkingService.save(park);
        return Result.success();
    }
}
